/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.constant.enums.ShopUserTypeEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.core.util.wechat.WeChatSignatureReqDTO;
import com.pig4cloud.pigx.common.core.util.wechat.WechatGzhAuth;
import com.pig4cloud.pigx.common.core.util.wechat.WechatUtils;
import com.pig4cloud.pigx.common.excel.annotation.RequestExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.order.api.entity.OrderInfo;
import com.pig4cloud.pigx.shop.user.api.dto.ShopUserDTO;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUser;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserInfo;
import com.pig4cloud.pigx.shop.user.api.vo.ShopUserExcelVO;
import com.pig4cloud.pigx.shop.user.api.vo.ShopUserSalesExcelVO;
import com.pig4cloud.pigx.shop.user.service.ShopUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商城用户
 *
 * @author pigx code generator
 * @date 2023-11-30 19:47:26
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Tag(description = "shop_user", name = "商城用户管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopUserController {

	private final ShopUserService shopUserService;

	/**
	 * 分页查询用户
	 * @param page 分页对象
	 * @param shopUserDTO 商城用户
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getShopUserPage(Page page, ShopUserDTO shopUserDTO) {
		return R.ok(shopUserService.getPage(page, shopUserDTO));
	}

	/**
	 * 通过id查询商城用户
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(shopUserService.getShopUserInfo(id));
	}

	/**
	 * 新增商城用户
	 * @param shopUser 商城用户
	 * @return R
	 */
	@Operation(summary = "新增商城用户", description = "新增商城用户")
	@SysLog("新增商城用户")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop_user_add')")
	public R save(@RequestBody ShopUser shopUser) {
		return R.ok(shopUserService.save(shopUser));
	}

	/**
	 * 修改商城用户
	 * @param shopUser 商城用户
	 * @return R
	 */
	@Operation(summary = "修改商城用户", description = "修改商城用户")
	@SysLog("修改商城用户")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop_user_edit')")
	public R updateById(@RequestBody ShopUser shopUser) {
		return R.ok(shopUserService.updateById(shopUser));
	}

	/**
	 * 通过id删除商城用户
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除商城用户", description = "通过id删除商城用户")
	@SysLog("通过id删除商城用户")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('shop_user_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(shopUserService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param shopUser 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('shop_user_export')")
	public List<ShopUser> export(ShopUser shopUser) {
		return shopUserService.list(Wrappers.query(shopUser));
	}

	/**
	 * c端用户注册
	 * @return
	 */
	@Inner(false)
	@PostMapping("/c/register")
	public R register(@RequestBody ShopUserDTO shopUserDTO) {
		String msg = shopUserService.register(shopUserDTO);
		if (StringUtils.isEmpty(msg)) {
			return R.ok();
		}
		else {
			return R.failed(msg);
		}
	}

	/**
	 * 获取用户信息
	 * @param loginAccount
	 * @return
	 */
	@Inner
	@GetMapping("/shop_user_info/{loginAccount}")
	public R getShopUserInfo(@PathVariable String loginAccount) {
		LambdaQueryWrapper<ShopUser> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShopUser::getLoginAccount, loginAccount);
		lqw.eq(ShopUser::getDelFlag, DeleteFlagEnum.VALID.getType());
		return R.ok(shopUserService.getOne(lqw));
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping("/c/info")
	public R getShopUserInfoByC() {
		Long shopUserId = SecurityUtils.getUser().getId();
		return R.ok(shopUserService.getShopUserInfoByC(shopUserId));
	}

	/**
	 * 内部获取用户信息
	 * @return
	 */
	@Inner
	@GetMapping("/info/inner/{shopUserId}")
	public R getShopUserInfoInner(@PathVariable Long shopUserId) {
		return R.ok(shopUserService.getShopUserInfoByC(shopUserId));
	}

	/**
	 * 导出销售代表空模板
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/exportUserSalesTemp")
	@PreAuthorize("@pms.hasPermission('shop_user_sales_export')")
	public List<ShopUserSalesExcelVO> exportUserSalesTemp() {
		List<ShopUserSalesExcelVO> result = new ArrayList<>();
		ShopUserSalesExcelVO vo = new ShopUserSalesExcelVO();
		vo.setRealName("");
		vo.setLoginAccount("");
		result.add(vo);
		return result;
	}

	/**
	 * 导出销售代表空模板
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/exportUserTemp")
	@PreAuthorize("@pms.hasPermission('shop_user_export')")
	public List<ShopUserExcelVO> exportUserTemp() {
		List<ShopUserExcelVO> result = new ArrayList<>();
		ShopUserExcelVO vo = new ShopUserExcelVO();
		vo.setLoginAccount("");
		vo.setPointBalance("0");
		vo.setRealName("");
		vo.setSalesShopUserIdLoginAccount("");
		result.add(vo);
		return result;
	}

	/**
	 * 导入销售代表
	 * @param excelVOList
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("importUserSales")
	@PreAuthorize("@pms.hasPermission('shop_user_sales_export')")
	public R importUserSales(@RequestExcel List<ShopUserSalesExcelVO> excelVOList, BindingResult bindingResult) {
		return shopUserService.importUserSales(excelVOList, bindingResult);
	}

	/**
	 * 导入销售代表
	 * @param excelVOList
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("importUser")
	@PreAuthorize("@pms.hasPermission('shop_user_export')")
	public R importUser(@RequestExcel List<ShopUserExcelVO> excelVOList, BindingResult bindingResult) {
		return shopUserService.importUser(excelVOList, bindingResult);
	}

	/**
	 * 获取微信openId
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/wechatAuthorize/{code}")
	public R wechatAuthorize(@PathVariable(value = "code") String code) {
		return R.ok(WechatUtils.authorize(code));
	}

	/**
	 * 获取微信openId
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getWechatPhone/{code}")
	public R getWechatPhone(@PathVariable(value = "code") String code) {
		return R.ok(WechatUtils.getPhone(code));
	}

	/**
	 * 修改用户昵称
	 * @return
	 */
	@PostMapping("/c/updateUserNickName")
	public R updateUserNickName(@RequestBody ShopUser shopUser) {
		Long shopUserId = SecurityUtils.getUser().getId();
		shopUser.setId(shopUserId);
		return shopUserService.updateUserNickName(shopUser);
	}

	/**
	 * 修改用户头像
	 * @return
	 */
	@PostMapping("/c/updateUserAvatar")
	public R updateUserAvatar(@RequestBody ShopUser shopUser) {
		Long shopUserId = SecurityUtils.getUser().getId();
		shopUser.setId(shopUserId);
		return shopUserService.updateUserAvatar(shopUser);
	}

	/**
	 * 根据销售id获取用户列表
	 * @param shopUserDTO
	 * @return
	 */
	@GetMapping("/getUserBySaleUserId")
	@PreAuthorize("@pms.hasPermission('shop_user_sales_association')")
	public R getUserBySaleUserId(Page page, ShopUserDTO shopUserDTO) {
		return R.ok(shopUserService.getUserPageListBySaleUserId(page, shopUserDTO));
	}

	/**
	 * 微信jsapi授权
	 * @param shopUserDTO
	 * @return
	 */
	@Inner(false)
	@PostMapping("/c/findWxJsSignature")
	public R wxJsSignature(@RequestBody ShopUserDTO shopUserDTO) {
		return shopUserService.getWeChatSignature(shopUserDTO);
	}

}
